package com.topisv.tms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.repository.EdiOrderItemDao;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class EdiOrderItemService {

	@Autowired
	private EdiOrderItemDao ediOrderItemDao;
	
	public List<EdiOrderItem> findByOrdeno(String ordeno){
		return ediOrderItemDao.findByOrdeno(ordeno);
	}
	
	public List<EdiOrderItem> findByCustomerCodeAndOrdeno(String customerCode,String ordeno){
		return ediOrderItemDao.findByCustomerCodeAndOrdeno(customerCode, ordeno);
	}

	@Transactional(readOnly = false)
	public void updateItems(String ordenoItems) throws Exception {

		try {
			String[] ordenoItemLists = ordenoItems.split("---paiu---");
			for (String item : ordenoItemLists) {
				String[] items = item.split("===paiu===");
				EdiOrderItem ediOrderItem = ediOrderItemDao.findOne(Long.parseLong(items[0]));
				ediOrderItem.setOrdeno(items[1]);
				ediOrderItem.setWaybillNo(items[2]);
				ediOrderItem.setStoreCode(items[3]);
				ediOrderItem.setSku(items[4]);
				ediOrderItem.setQty(Integer.parseInt(items[5]));
				ediOrderItemDao.save(ediOrderItem);
			}
		} catch (Exception e) {
			throw e;
		}
	}

	public List<EdiOrderItem> findByCrtimeBetween(Date fromDate, Date endDate) {
		return ediOrderItemDao.findByCrtimeBetween(fromDate, endDate);
	}

	@Transactional(readOnly = false)
	public EdiOrderItem save(EdiOrderItem arg0) {
		return ediOrderItemDao.save(arg0);
	}

	public EdiOrderItem findOne(Long arg0) {
		return ediOrderItemDao.findOne(arg0);
	}

	@Transactional(readOnly = false)
	public void delete(Long arg0) {
		ediOrderItemDao.delete(arg0);
	}

	@Transactional(readOnly = false)
	public List<EdiOrderItem> save(List<EdiOrderItem> arg0) {
		return (List<EdiOrderItem>) ediOrderItemDao.save(arg0);
	}

	public List<EdiOrderItem> findAll() {
		return (List<EdiOrderItem>) ediOrderItemDao.findAll();
	}
}
